const RegistrationService = require('../services/registration.service')
const registrationService = new RegistrationService()

const ReportService = require('../services/report.service')
const reportService = new ReportService()

const AreaService = require('../services/area.service')
const areaService = new AreaService()


class StatisticsController {
  async reportrate (ctx, next) {
    const query = ctx.request.query
    const data = []
    const allAreas = await areaService.findAll()
    for (let i = 0; i < allAreas.length; i++) {
      let area = allAreas[i]
      let item = {
        area: area
      }
      const allPlaces = await registrationService.findAll({
        reg_status: 'resolve',
        reg_xzxq: area.code,
        pageSize: 999999
      })
      const allReports = allPlaces.data.length ? await reportService.findAll({
        place: allPlaces.data.map(i => i.place._id).join(','),
        submitDate: {
          $gte: new Date(query.start),
          $lte: new Date(query.end)
        }
      }) : { data: [] } // todo 一天上报多次未处理
      item.placeCount = allPlaces.data.length
      item.reportCount = allReports.data.length
      data.push(item)
    }
    ctx.response.body = data
  }
  async reportstatus (ctx, next) {
    const query = ctx.request.query
    const data = []
    const allAreas = await areaService.findAll()
    for (let i = 0; i < allAreas.length; i++) {
      let area = allAreas[i]
      let item = {
        area: area
      }
      const allPlaces = await registrationService.findAll({
        reg_status: 'resolve',
        reg_xzxq: area.code,
        pageSize: 999999
      })
      const allReports = allPlaces.data.length ? await reportService.findAll({
        place: allPlaces.data.map(i => i.place._id).join(','),
        submitDate: {
          $gte: new Date(query.start),
          $lte: new Date(query.end)
        }
      }) : { data: [] } // todo 一天上报多次未处理
      item.placeCount = allPlaces.data.length
      item.reportCount = allReports.data.length
      item.normalReportCount = allReports.data.filter(i => !i.unpassedItems.length).length
      data.push(item)
    }

    ctx.response.body = data
  }
}

module.exports = StatisticsController